package fr.imag.adele.cprobes.abstracts;

import fr.imag.adele.cprobes.impl.StandardScheduler;
import fr.imag.adele.cprobes.services.DataCollector;
import fr.imag.adele.cprobes.services.DataSender;
import fr.imag.adele.cprobes.services.Probe;
import fr.imag.adele.cprobes.services.Scheduler;
import fr.imag.adele.cprobes.util.DataSet;

public abstract class AbstractProbe implements Probe {
	private String 			m_probeId;			// probe id (it should be unique)
	private DataCollector 	m_dataCollector;	// data collector component that collect the data events
	private DataSender 		m_dataSender;		// data sender component that send the processed data
	private Scheduler 		m_scheduler;		// scheduler that control when call the process method
	private boolean 		m_working;			// indicate wherever the component is working or not
		
	/**
	 * get the collected data to be processed locally
	 */
	public DataSet getCollectedData() {
		if (getDataCollector() != null)
			return getDataCollector().getCollectedData();
		else 
			return null;
	}		
	/**
	 * Send the processed data to the interesting targets
	 */
	public void sendProcessedData(DataSet dataSet) {
		dataSet = handleData(dataSet);
		if (getDataSender() != null)
			getDataSender().sendData(dataSet);
	}
	/**
	 * Called when the component started.
	 */
	public void starting() {
		System.out.println("Starting probe: " + getProbeId());
		
		if (getDataCollector() != null) getDataCollector().setProbe(this);
		if (getDataSender() != null) getDataSender().setProbe(this);
		if (getScheduler() != null) getScheduler().setProbe(this);
		
		m_working=true;	
	}
	/**
	 * Called when the component stopped.
	 */
	public void stoping() {
		m_working=false;	
	}
	/**
	 * Change all the processed data to have the current probe as source,
	 * and affect it a new timestamp.
	 * 
	 * @param dataSet list of DataEvents
	 * @return handled DataEvents
	 */
	private DataSet handleData(DataSet dataSet){		
		if (dataSet != null){
			for (int i=0;i<dataSet.size(); i++){
				dataSet.getDataEvent(i).setSource(m_probeId);
				dataSet.getDataEvent(i).setTimeStamp(new java.util.Date().toString());
			}			
		} 
		return dataSet;
	}
	
	//// GETTERS and SETTERS
	
	public String getProbeId() {
		return m_probeId;
	}
	public void setProbeId(String probeId) {
		m_probeId = probeId;
	}
	public Scheduler getScheduler() {
		return m_scheduler;
	}	
	public void setScheduler(Scheduler scheduler) {
		m_scheduler = scheduler;
	}		
	public DataCollector getDataCollector() {
		return m_dataCollector;
	}	
	public void setDataCollector(DataCollector dataCollector) {
		this.m_dataCollector = dataCollector;
	}
	public DataSender getDataSender() {
		return m_dataSender;
	}
	public void setDataSender(DataSender dataSender) {
		this.m_dataSender = dataSender;
	}		
	public void setWorking(boolean working) {
		this.m_working = working;		
	}
	public boolean isWorking() {
		return m_working;
	}
	//////
}
